package com.pkk.cloud.support.oauth2.common.utils;

import com.pkk.cloud.support.admin.oauth2.api.common.resp.BasicUserInfo;
import com.pkk.components.moudle.oauth2.AuthUserException;
import com.pkk.components.rpc.common.code.AuthoritiesCode;

/**
 * @Description: Security工具类
 * @Author: peikunkun
 * @Date: 2020/3/21 0021 下午 3:09
 */
public class SecurityUtils {

  /**
   * @Description: 获取token的用户信息的基础信息
   * @return: com.pkk.cloud.support.admin.oauth2.api.common.resp.BasicUserInfo
   * @Author: peikunkun
   * @Date: 2020/3/23 0023 下午 2:54
   */
  public static BasicUserInfo getUserInfo() {
    BasicUserInfo user = UserInfoUtil.getUserInfo(BasicUserInfo.class);
    if (user != null) {
      return user;
    }
    throw new AuthUserException(AuthoritiesCode.CLIENT_VALID_TOKEN_ERROR);
  }

  /**
   * @Description: 从token获取的用户信息之用户名称
   * @return: java.lang.String
   * @Author: peikunkun
   * @Date: 2020/3/23 0023 下午 2:53
   */
  public static String getUsername() {
    return getUserInfo().getUsername();
  }

  /**
   * @Description: 从token获取的用户信息之用户id
   * @return: java.lang.String
   * @Author: peikunkun
   * @Date: 2020/3/23 0023 下午 2:53
   */
  public static String getUserId() {
    return getUserInfo().getId();
  }


  /**
   * @Description:获取并转换userId信息【部分项目userId为数字类型可用】
   * @return: java.lang.Long
   * @Author: peikunkun
   * @Date: 2020/3/26 0026 下午 2:28
   */
  public static Long getUserIdLong() {
    return Long.valueOf(getUserInfo().getId());
  }


}
